package top.chenyaohua.visitorImp;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取目录下的文件或文件夹
 */
public class ClassFileVisitor extends SimpleFileVisitor<Path> {

    private List<File> dirs = new ArrayList<>();

    private List<File> files = new ArrayList<>();

    private List<Path> paths = new ArrayList<>();

    @Override
    public FileVisitResult preVisitDirectory (Path dir, BasicFileAttributes attrs) throws IOException {

        this.dirs.add(dir.toFile());

        return super.preVisitDirectory(dir, attrs);
    }

    @Override
    public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {

        File fs = file.toFile();

        if (fs.getName().endsWith(".class")) {
            this.files.add(fs);
            this.paths.add(file);
        }

        return super.visitFile(file, attrs);
    }

    @Override
    public FileVisitResult visitFileFailed (Path file, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }


    public List<File> getDirs () {
        return dirs;
    }

    public List<File> getFiles () {
        return files;
    }

    public List<Path> getPaths () {
        return paths;
    }
}
